Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
'Imports DotNetNuke.Installer
Imports ICSharpCode.SharpZipLib.Zip
Imports System.IO
Imports DotNetNuke
Namespace NVPortal.Modules.NVNews

    Public MustInherit Class Guibaiviet
        Inherits Entities.Modules.PortalModuleBase
        Dim tin As Integer

#Region "Event Handlers"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not IsPostBack Then
                Try
                    BindddlCategories()
                    BindNguonTin()
                    '<Edit news>
                    If Request.Item("tin") <> "" Then
                        Dim tin As Integer
                        tin = CInt(Request.Item("tin"))
                        ' Hien thi tin nay
                        Dim ctlNews As New NV_NewsController
                        Dim objNews As NV_NewsInfo
                        objNews = ctlNews.GetByID(Request.Item("tin"))
                        Me.txtTitle.Text = objNews.Title
                        Me.txtSummary.Text = objNews.Summary
                        Me.TextEditor1.Text = objNews.Content
                        Me.txtLinkNguon.Text = objNews.LinkNguon
                        If Not objNews.ImagePath Is Nothing Then
                            If objNews.ImagePath.ToString.Contains("http://") Then
                                Me.txtLinkImage.Text = objNews.ImagePath
                            Else
                            End If
                        End If
                        If Not objNews.ImagePathHOT Is Nothing Then
                            If objNews.ImagePathHOT.ToString.Contains("http://") Then
                                Me.txtLinkImageHOT.Text = objNews.ImagePathHOT
                            Else
                            End If
                        End If
                        Me.ddlCategory.SelectedValue = objNews.CategoryId
                        Me.ddlNguonTin.SelectedValue = objNews.Nguon
                    Else
                    End If

                    '</Edit news>
                Catch ex As Exception
                    ProcessModuleLoadException(Me, ex)
                End Try
            End If
        End Sub
        Private Sub lbtUpdateTop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtUpdateTop.Click
            If addnews() Then
                'clearcontroldata()
                'addnewsvideo("idtintuc")
            End If
        End Sub
        Private Function addnews() As Boolean
            Try
                If Request.Item("tin") <> "" Then
                    Dim tin As Integer
                    tin = CInt(Request.Item("tin"))
                    'update news
                    Dim stitle As String
                    Dim ssummary As String
                    Dim scontent As String
                    Dim spicture As String
                    Dim spictureHOT As String
                    Dim iCategory As Integer
                    Dim iHotsite As Integer
                    Dim iNotes As Integer
                    Dim iHotCat As Integer
                    Dim sExSummary As String
                    Dim sConTentImg As String
                    Dim iBL As Integer
                    Dim inguon As Integer
                    ' Insert
                    stitle = Me.txtTitle.Text
                    ssummary = Me.txtSummary.Text
                    scontent = TextEditor1.Text
                    sConTentImg = txtLinkNguon.Text
                        spicture = txtLinkImage.Text
                        spictureHOT = txtLinkImageHOT.Text
                    iCategory = CInt(Me.ddlCategory.SelectedItem.Value)
                    inguon = CInt(Me.ddlNguonTin.SelectedItem.Value)
                    DataProvider.Instance.NV_News_update(tin, iCategory, stitle, spicture, ssummary, scontent, 1, 1, 1, 1, PortalId, "", sConTentImg, spictureHOT, 1, inguon)
                    Response.Redirect(NavigateURL(), True)
                Else
                    Dim stitle As String
                    Dim ssummary As String
                    Dim scontent As String
                    Dim spicture As String
                    Dim spictureHOT As String
                    Dim iCategory As Integer
                    Dim iHotsite As Integer
                    Dim iNotes As Integer
                    Dim iHotCat As Integer
                    Dim sExSummary As String
                    Dim sConTentImg As String
                    Dim iBL As Integer
                    Dim inguon As Integer
                    ' Insert
                    stitle = Me.txtTitle.Text
                    ssummary = Me.txtSummary.Text
                    scontent = TextEditor1.Text
                    sConTentImg = txtLinkNguon.Text

                        spicture = txtLinkImage.Text
                    spictureHOT = txtLinkImageHOT.Text
                    iCategory = CInt(Me.ddlCategory.SelectedItem.Value)
                    inguon = CInt(Me.ddlNguonTin.SelectedItem.Value)
                    'isactive = CInt(IIf(Me.chkHotSite.Checked(), 1, 0))
                    DataProvider.Instance.NV_News_Add(iCategory, stitle, spicture, ssummary, scontent, 1, 1, 1, 1, PortalId, UserId, "", sConTentImg, spictureHOT, 1, inguon)
                    Me.lbResult.Text = "Cập nhật tin thành công"
                    Response.Redirect(NavigateURL(), True)
                End If
                Return True
            Catch ex As Exception
                Me.lbResult.Text = "Lỗi cập nhật tin"
                ProcessModuleLoadException(Me, ex)
                Return False
            End Try
        End Function
        Private Sub lbtUpdateBottom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtUpdateBottom.Click
            If addnews() Then
                clearcontroldata()
                ' addnewsvideo("idtintuc")
            End If
        End Sub
        Private Sub lbtCancelTop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtCancelTop.Click
            Try
                If Request.Item("tin") <> "" Then
                    'Dim tin As Integer
                    'tin = CInt(Request.Item("tin"))
                    Response.Redirect(NavigateURL(), True)
                Else
                    clearcontroldata()
                End If
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Private Sub clearcontroldata()
            Me.txtSummary.Text = ""
            Me.txtTitle.Text = ""
            TextEditor1.Text = "Đang cập nhật"
        End Sub
#End Region
        Private Sub BindddlCategories()
            Dim ctlNewsCategories As New NV_NewsCategoriesController
            Dim arrNewsCategories As New ArrayList
            arrNewsCategories = ctlNewsCategories.GetAll(PortalId)
            Dim arrTemp As New ArrayList
            Dim objNewsCategories As NV_NewsCategoriesInfo
            Dim objNewsCategoriesTemp As NV_NewsCategoriesInfo

            If arrNewsCategories.Count > 0 Then
                For Each objNewsCategories In arrNewsCategories
                    If objNewsCategories.ParentId = 0 Then
                        arrTemp.Add(objNewsCategories)
                        For Each objNewsCategoriesTemp In arrNewsCategories
                            If objNewsCategoriesTemp.ParentId = objNewsCategories.CategoryID Then
                                objNewsCategoriesTemp.CategoryName = "||---->" & objNewsCategoriesTemp.CategoryName
                                arrTemp.Add(objNewsCategoriesTemp)
                            End If
                        Next
                    End If
                Next
            End If

            Me.ddlCategory.DataSource = arrTemp
            Me.ddlCategory.DataTextField = "CategoryName"
            Me.ddlCategory.DataValueField = "CategoryId"
            Me.ddlCategory.DataBind()
        End Sub
        Public Function GetUserName(ByVal userid As Integer) As String
            If userid = -1 Then
                GetUserName = "Bạn đọc Gửi"
            Else
                Return UserController.GetUser(PortalId, userid, True).Username
            End If
        End Function
        'TinLienQuan
        Private Sub BindNguonTin()
            Dim ctlNewRelated As New NV_NewsNguonController
            Me.ddlNguonTin.DataSource = ctlNewRelated.GetAll()
            Me.ddlNguonTin.DataValueField = "id"
            Me.ddlNguonTin.DataTextField = "Title"
            Me.ddlNguonTin.DataBind()
        End Sub
    End Class

End Namespace
